<?php

class dbaModule
{

    function dbaModule(&$dba)
    {
        $this->dba = &$dba;
    }

    function addInstance($mod,$name,$site_id)
    {
        $this->dba->query("insert into ".$this->dba->tableforwarder."_modInstance (module,name,site_id) values ('$mod','$name','$site_id')");
    }
    
    function updateInstance($name,$site_id)
    {
        $this->dba->query("update ".$this->dba->tableforwarder."_modInstance set site_id='$site_id' where name='$name';");
    }
    
    function delInstance($name,$mod)
    {
        global $cfgPaths;
        $this->dba->query("delete from ".$this->dba->tableforwarder."_modInstance where name='$name';");
        $this->dba->query("delete from ".$this->dba->tableforwarder."_Permission where name='${name}s_%';");
        include($cfgPaths['mod'].$mod.'/etc/tables.inc.php');
        if (is_array($tables))
        {
	        foreach ($tables As $table)
    	    {
         		$sth = $this->dba->query("show columns from ".$this->dba->tableforwarder."_$table;");
         		if (is_object($sth))
				{
         			$tmp = $sth->fetch_rows();
         			$flag = false;
         			foreach ($tmp As $t) if ($t[0]=='instance') $flag = true;
         			if ($flag)
    	            	$this->dba->query("delete from ".$this->dba->tableforwarder."_$table where instance='$name';");
    	        }
    	    }
        }
    }
    
    function getInstance($name)
    {
        $sth = $this->dba->query("select * from ".$this->dba->tableforwarder."_modInstance where name='$name';");
        if (is_object($sth))
        {
            return $sth->fetch_row_Array();
        }
    }
    
    function getByModule($mod)
    {
        $sth = $this->dba->query("select * from ".$this->dba->tableforwarder."_modInstance where module='$mod';");
        if (is_object($sth))
        {
            return $sth->fetch_rows_Array();
        }
    }
    
    function getAll()
    {
        $sth = $this->dba->query("select * from ".$this->dba->tableforwarder."_modInstance order by module,name;");
        if (is_object($sth))
        {
            return $sth->fetch_rows_Array();
        }
    }

}

?>